package com.wuye.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import lombok.Getter;
import lombok.Setter;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.wuye.common.util.string.StrUtil;
import com.wuye.common.vo.RetVO;
import com.wuye.constants.BaseConstants;
import com.wuye.entity.Role;
import com.wuye.entity.User;
import com.wuye.services.LoginServiceManager;
import com.wuye.services.RoleServiceManager;

public class LoginAction extends ActionSupport{
	private static Logger log = Logger.getLogger(LoginAction.class);
	@Autowired
	private LoginServiceManager loginServiceManager;
	@Autowired
	private RoleServiceManager roleServiceManager;
	private String  account;
	private String tip = "";
	private String  password;
	@Setter
	@Getter
	private String autoSecurityCode;
	private boolean yjdl;
	private boolean logout_suc;
	
	public String checkUser() {
		User user = (User) ActionContext.getContext().getSession().get("user");
		if (user == null) {
			yjdl = false;
		} else {
			yjdl = true;
		}
		return "yjdl";
	}
	
	public String execute() throws Exception{
		User  user=null;
		ActionContext ctx = ActionContext.getContext();
		Map<String,Object> map = new HashMap<String, Object>();
		Object securityCode = ctx.getSession().get("securityCode");
		if (!StrUtil.isNullOrEmpty(autoSecurityCode) && !StrUtil.isNullOrEmpty(securityCode) && autoSecurityCode.equals(securityCode.toString())) {
			try{
				RetVO ret =loginServiceManager.login2(account, password);
				
				
				if(BaseConstants.RET_TRUE.equals(ret.getResult())){
					Map<String,Object> retObj = (Map<String,Object>)ret.getObject();
					user = (User)retObj.get("user");
					ctx.getSession().clear();
					ctx.getSession().put("user", retObj.get("user"));
					
					if(retObj.get("companyId")!=null){
						ctx.getSession().put("companyId", retObj.get("companyId"));
					}
					if(retObj.get("companyName")!=null){
						ctx.getSession().put("companyName", retObj.get("companyName"));
					}
					if(retObj.get("communityId")!=null){
						ctx.getSession().put("communityId", retObj.get("communityId"));
						ctx.getSession().put("communityName", retObj.get("communityName"));
					}
					if(retObj.get("orgId")!=null){
						ctx.getSession().put("orgId", retObj.get("orgId"));
					}
					List<Role> roleList = roleServiceManager.getRoleListByUserId(user.getId());
					ctx.getSession().put("roleList", roleList);
					map.put("result", true);
					map.put("msg", "登录成功");
				}else{
					map.put("result", false);
					map.put("msg", "登录失败");
				}
			}catch(Exception e){
				//e.printStackTrace();
				log.info(e.getMessage());
				map.put("result", false);
				map.put("msg", "登录失败");
			}
		} else {
			map.put("result", null);
			map.put("msg", "验证码错误");
		}
		
		JSONObject json = JSONObject.fromObject(map);
		tip = json.toString();
		return SUCCESS;
	}

	/**
	 * 退出功能
	 * @return
	 */
	public String logoutFun() {
		ActionContext ctx = ActionContext.getContext();
		ctx.getSession().clear();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("result", true);
		JSONObject json = JSONObject.fromObject(map);
		tip = json.toString();
		return SUCCESS;
	}
	public LoginServiceManager getLoginServiceManager() {
		return loginServiceManager;
	}



	public void setLoginServiceManager(LoginServiceManager loginServiceManager) {
		this.loginServiceManager = loginServiceManager;
	}



	public String getAccount() {
		return account;
	}



	public void setAccount(String account) {
		this.account = account;
	}



	public String getTip() {
		return tip;
	}



	public void setTip(String tip) {
		this.tip = tip;
	}



	public String getPassword() {
		return password;
	}



	public void setPassword(String password) {
		this.password = password;
	}

	public boolean isYjdl() {
		return yjdl;
	}

	public void setYjdl(boolean yjdl) {
		this.yjdl = yjdl;
	}

	public boolean isLogout_suc() {
		return logout_suc;
	}

	public void setLogout_suc(boolean logoutSuc) {
		logout_suc = logoutSuc;
	}
	
}
